package com.test;

import com.google.gson.Gson;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Base64;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class K {

    public enum APLMakeupContourAreaType {
        APLMakeupContourAreaType_Unknown,
        APLMakeupContourAreaType_Forehead,
        APLMakeupContourAreaType_Cheek,
        APLMakeupContourAreaType_Nose,
        APLMakeupContourAreaType_Chin;

        APLMakeupContourAreaType() {
        }
    }

    public static final int TYPE_VIEW_CLICKED = 0x00000001;
    public static final int TYPE_VIEW_LONG_CLICKED = 0x00000002;
    public static final int TYPE_VIEW_TEXT_CHANGED = 0x00000010;
    public static final int TYPE_WINDOW_STATE_CHANGED = 0x00000020;

    public static final int TYPE_WINDOW_CONTENT_CHANGED = 0x00000800;

    private static final char[] charSet = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
    };

    @Test
    public void testCalandar() {
        String colorString = ("#FEF3E2");

        long color = Long.parseLong(colorString.substring(1), 16);
        System.out.println(color);
        int[] source = new int[5];
        for (int i = 5; i > 0; i--) {
            source[5 - i] = i;
        }
        Gson gson = new Gson();
        System.out.println(gson.toJson(source));

        int[] temp = new int[source.length + 1];
        System.arraycopy(source, 0, temp, 0, source.length);
        temp[temp.length - 1] = 0;
        source = temp;
        System.out.println(gson.toJson(source));
        APLMakeupContourAreaType value = APLMakeupContourAreaType.values()[2];
        System.out.println(value);
        System.out.println(to64StringSimple(Long.MAX_VALUE));
        long t1 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            to64StringSimple(System.nanoTime());
        }
        System.out.println("to64StringSimple spend  " + (System.nanoTime() - t1));
        t1 = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            to62String(System.nanoTime());
        }

        System.out.println("to62String spend  " + (System.nanoTime() - t1));

        System.out.println(Base64.getEncoder().encodeToString("1234567".getBytes()));
        System.out.println(TYPE_VIEW_CLICKED);
        System.out.println(TYPE_VIEW_LONG_CLICKED);
        System.out.println(TYPE_VIEW_TEXT_CHANGED);

        System.out.println(TYPE_WINDOW_STATE_CHANGED);
        System.out.println(TYPE_WINDOW_CONTENT_CHANGED);

        System.out.println("------------------");
        Calendar calendar = Calendar.getInstance();

        System.out.println(calendar.get(Calendar.MONTH) + " " + calendar.get(Calendar.DAY_OF_MONTH));

    }

    public static int findFirstIndexNumberOfStr(String str) {
        int i = -1;
        Matcher matcher = Pattern.compile("[0-9]").matcher(str);
        if (matcher.find()) {
            i = matcher.start();
            System.out.println("end " + matcher.end() + "," + i);
        }
        return i;
    }


    @Test
    public void testA() {
        String str = "US$1.99";
      /*  System.out.println(str.substring(findFirstIndexNumberOfStr(str)));*/
//        System.out.println(1 + (1 << 16));
//        System.out.println(1 << 13);
    }

    public static int parseColor(String colorString) {
        if (colorString.charAt(0) == '#') {
            // Use a long to avoid rollovers on #ffXXXXXX
            long color = Long.parseLong(colorString.substring(1), 16);
            if (colorString.length() == 7) {
                // Set the alpha value
                color |= 0x00000000ff000000;
            } else if (colorString.length() != 9) {
                throw new IllegalArgumentException("Unknown color");
            }
            return (int) color;
        }
        throw new IllegalArgumentException("Unknown color");
    }

    public static String to16String(long args) {
        long rest = args;
        StringBuilder stringBuffer = new StringBuilder();
        while (rest != 0) {
            Character character = charSet[(int) (rest % 16)];
            stringBuffer.append(character);
            rest = rest / 16;
        }
        return stringBuffer.reverse().toString();
    }

    public static String to62String(long args) {
        long rest = args;
        StringBuilder stringBuffer = new StringBuilder();
        while (rest != 0) {
            Character character = charSet[(int) (rest % 62)];
            stringBuffer.append(character);
            rest = rest / 62;
        }
        return stringBuffer.reverse().toString();
    }

    public static String to64String(long args) {
        long rest = args;
        StringBuilder stringBuffer = new StringBuilder();
        while (rest != 0) {
            Character character = charSet[(int) (rest % 64)];
            stringBuffer.append(character);
            rest = rest / 64;
        }
        return stringBuffer.reverse().toString();
    }

    public static String to64StringSimple(long args) {
        long rest = args;
        StringBuilder stringBuffer = new StringBuilder();
        while (rest != 0) {
            Character character = charSet[(int) (rest & 0x3F)];
            stringBuffer.append(character);
            rest = rest >> 6;
        }
        return stringBuffer.reverse().toString();
    }

    @Test
    public void byteArray() throws IOException {
        String str = "hello";
        byte[] array = str.getBytes();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(array);
        outputStream.flush();
        byte[] array2 = outputStream.toByteArray();

        if (array.length != array2.length) {
            System.out.println("XXXX");
            return;
        }

        for (int i = array.length - 1; i >= 0; i--) {
            if (array[i] != array2[i]) {
                System.out.println("XXXX");
                break;
            }
        }
        System.out.println("over");


        for (int i = 0; i < 10; i++) {
            System.out.println(new ObjectId().toString());
        }
        System.out.println(new ObjectId().hashCode());
        ;
    }

    @Test
    public void testInet() {
        try {
            InetAddress ia = InetAddress.getLocalHost();
            System.out.println(ia.getHostAddress());
            System.out.println(ia.getHostName());
            System.out.println(ia.getCanonicalHostName());
            System.out.println(ia.toString());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


}
